﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Activities;
using Microsoft.QualityTools.Testing.Fakes;
using HybridLab.Virtual.UnitTests.Utils;
using HybridLab.Workflow.Activities;
using HybridLab.Workflow.Activities.Info;
using System.Collections.Generic;
using HybridLab.Virtual.Interfaces.Fakes;

namespace HybridLab.Virtual.UnitTests
{
	[TestClass]
	public class TestRestoreHybridLabSnapshot
	{
		[TestMethod]
		public void TestRestoreHybridLabSnapshotActivity()
		{
			using (var context = ShimsContext.Create())
			{
				var testWorkflow = new TestRestoreHybridLabSnapshotWorkflow();
				var workflowInvoker = new WorkflowInvoker(testWorkflow);

				var snapshots = TestUtil.GetFakeSnapshots();
				var appliedSnapshots = new Dictionary<string, string>();
				ShimVirtualHostCatalog.GetHostStringStringStringStringString = (t, h, u, d, p) =>
				{
					return TestUtil.GetFakeHost(snapshots, appliedSnapshots);
				};

				var snapshotInfo = new HybridLabEnvironmentSnapshotInfo()
				{
					Snapshots = new List<HybridLabVMSnapshotInfo>()
					{
						new HybridLabVMSnapshotInfo() { MachineName = "TestMachine1", SnapshotName = "Clean" },
						new HybridLabVMSnapshotInfo() { MachineName = "TestMachine2", SnapshotName = "Clean" },
					}
				};

				// set the args with fakes
				var details = new HybridLabWorkflowDetails()
				{
					HybridLab = new HybridLabEnvironment() 
					{
						HostType = "HyperV",
						HostName = "localhost", 
						Credentials = new HybridLabHostCredentials() { UserName = "bob", Password = "pass" },
						EnvironmentSnapshotInfo = snapshotInfo 
					},
					DeploymentDetails = TestUtil.GetFakeDeploymentDetails()
				};
				testWorkflow.HybridLabWorkflowDetails = new InArgument<HybridLabWorkflowDetails>(c => details);

				// run the workflow
				var result = workflowInvoker.Invoke();

				// check that the snapshots were applied
				Assert.AreEqual(2, appliedSnapshots.Count);
				Assert.IsTrue(appliedSnapshots.Keys.Contains("TestMachine1"));
				Assert.AreEqual("Clean", appliedSnapshots["TestMachine1"]);
				Assert.IsTrue(appliedSnapshots.Keys.Contains("TestMachine2"));
				Assert.AreEqual("Clean", appliedSnapshots["TestMachine2"]);
			}
		}
	}
}
